package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.controller.PmListController;
import com.central.apps.handler.base.LvBaseHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
@Component
@Slf4j
public class PmlistHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private PmListController pmListController;

    public void pmToWorkorder() throws Exception {
        log.info("进入预防性维护定时任务++++++++++++++++++++++++++++++++++++");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date now =new Date();
        //查询所有勾选启动pm列
        List<Map<String, Object>> pmLists = handlerService.findBySql("select * from gq_pmlist where isaction ='1'");
        for(Map<String, Object> map :pmLists){
            String nexttime = MapUtils.getString(map,"nextexecutetime");
            map.put("nextexecutetime",nexttime);
            JSONObject jsonObject = JSONObject.fromObject(map);
            String id = MapUtils.getString(map,"id");
            //是否启用周期设置 isoncyel 1:y  0:n
            String isoncyel = MapUtils.getString(map,"isoncyel");
            if("1".equals(isoncyel)){
                //是否提前生成 isbefore  1:y  0:n
                String isbefore = MapUtils.getString(map,"isbefore");
                if("1".equals(isbefore)){
                    //提前天数 leadtime
                    String leadtime = MapUtils.getString(map,"leadtime");
                    List<Integer> weekLists =new ArrayList<>();
                    Integer Z7 = MapUtils.getInteger(map,"Z7"); weekLists.add(Z7);
                    Integer Z1 = MapUtils.getInteger(map,"Z1"); weekLists.add(Z1);
                    Integer Z2 = MapUtils.getInteger(map,"Z2"); weekLists.add(Z2);
                    Integer Z3 = MapUtils.getInteger(map,"Z3"); weekLists.add(Z3);
                    Integer Z4 = MapUtils.getInteger(map,"Z4"); weekLists.add(Z4);
                    Integer Z5 = MapUtils.getInteger(map,"Z5"); weekLists.add(Z5);
                    Integer Z6 = MapUtils.getInteger(map,"Z6"); weekLists.add(Z6);
                    Calendar calendar = Calendar.getInstance();
                    int week =calendar.get(Calendar.DAY_OF_WEEK);
                    List<Integer> acLists =new ArrayList<>();
                    for(int i=0;i< weekLists.size(); i++){
                        if(weekLists.get(i)==1){
                            acLists.add(i+1);
                        }
                    }
                    if(acLists.size() ==1){
                        int nowweek =acLists.get(0);
                        if(week <nowweek){
                            if(week ==nowweek-Integer.parseInt(leadtime)){
                                Date lastDate =now;
                                Date nextDate =getNextDate(now,7+Integer.parseInt(leadtime),"D");
                                handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                pmListController.PmCreateWorkorder(jsonObject);
                            }
                        }else{
                            if(week ==nowweek-Integer.parseInt(leadtime)+7){
                                Date lastDate =now;
                                Date nextDate =getNextDate(now,7-nowweek,"D");
                                handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                pmListController.PmCreateWorkorder(jsonObject);
                            }
                        }
                    }else{
                        for(int i =0; i<acLists.size(); i++){
                            int nowweek =acLists.get(i);
                            if(week <nowweek){
                                if(week ==nowweek-Integer.parseInt(leadtime)){
                                    Date lastDate =now;
                                    if(i!=acLists.size()-1){
                                        Date nextDate =getNextDate(now,acLists.get(i+1)-week,"D");
                                        handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                        pmListController.PmCreateWorkorder(jsonObject);
                                    }else{
                                        Date nextDate =getNextDate(now,acLists.get(0)-week+7,"D");
                                        handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                        pmListController.PmCreateWorkorder(jsonObject);
                                    }
                                }
                            }else{
                                if(week ==nowweek-Integer.parseInt(leadtime)+7){
                                    Date lastDate =now;
                                    if(i!=acLists.size()){
                                        Date nextDate =getNextDate(now,acLists.get(i+1)-week,"D");
                                        handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                        pmListController.PmCreateWorkorder(jsonObject);
                                    }else{
                                        Date nextDate =getNextDate(now,acLists.get(0)-week+7,"D");
                                        handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                        pmListController.PmCreateWorkorder(jsonObject);
                                    }
                                }
                            }
                        }
                    }
                }else{
                    List<Integer> weekLists =new ArrayList<>();
                    Integer Z7 = MapUtils.getInteger(map,"Z7"); weekLists.add(Z7);
                    Integer Z1 = MapUtils.getInteger(map,"Z1"); weekLists.add(Z1);
                    Integer Z2 = MapUtils.getInteger(map,"Z2"); weekLists.add(Z2);
                    Integer Z3 = MapUtils.getInteger(map,"Z3"); weekLists.add(Z3);
                    Integer Z4 = MapUtils.getInteger(map,"Z4"); weekLists.add(Z4);
                    Integer Z5 = MapUtils.getInteger(map,"Z5"); weekLists.add(Z5);
                    Integer Z6 = MapUtils.getInteger(map,"Z6"); weekLists.add(Z6);
                    Calendar calendar = Calendar.getInstance();
                    int week =calendar.get(Calendar.DAY_OF_WEEK);
                    List<Integer> acLists =new ArrayList<>();
                    for(int i=0;i< weekLists.size(); i++){
                        if(weekLists.get(i)==1){
                            acLists.add(i+1);
                        }
                    }
                    if(acLists.size() ==1){
                        if(week ==acLists.get(0)){
                            Date lastDate =now;
                            Date nextDate =getNextDate(now,7,"D");
                            handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                            pmListController.PmCreateWorkorder(jsonObject);
                        }
                    }else{
                        for(int i =0; i<acLists.size(); i++){  //4  6
                            if(week ==acLists.get(i)){
                                Date lastDate =now;
                                if(i!=acLists.size()-1){
                                    Date nextDate =getNextDate(now,acLists.get(i+1)-week,"D");
                                    handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                    pmListController.PmCreateWorkorder(jsonObject);
                                }else{
                                    Date nextDate =getNextDate(now,acLists.get(0)-week+7,"D");
                                    handlerService.updateBySql("update gq_pmlist set beforedate ='"+sdf.format(lastDate)+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                                    pmListController.PmCreateWorkorder(jsonObject);
                                }
                            }
                        }
                    }
                }
            }else{
                //获取下次执行时间  nextexecutetime
                String nextexecutetime = MapUtils.getString(map,"nextexecutetime");
                //取前10位,精确到天
                String nextexecutetimeDay =nextexecutetime.substring(0,10);
                if(sdf2.format(now).equals(nextexecutetimeDay)){
                    //频率 frequency
                    String frequency = MapUtils.getString(map,"frequency");
                    //频率单位 frequencyunit  D:day w:week m:month y:year
                    String frequencyunit = MapUtils.getString(map,"frequencyunit");
                    //是否根据实际生成时间计算下次到期日期 isskipweek 1:y  0:n
                    String isskipweek = MapUtils.getString(map,"isskipweek");
                    Date nextDate =null;
                    if("1".equals(isskipweek)){
                        nextDate = getNextDate(now, Integer.parseInt(frequency), frequencyunit);
                    }else{
                        nextDate = getNextDate(sdf.parse(nextexecutetime), Integer.parseInt(frequency), frequencyunit);
                    }
                    handlerService.updateBySql("update gq_pmlist set beforedate ='"+nextexecutetime+"',nextexecutetime ='"+sdf.format(nextDate)+"' where id ="+id);
                    pmListController.PmCreateWorkorder(jsonObject);
                }
            }
        }
    }

    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        //保存前，验证 周期性开关是否开启，如果开启，周一到周日必须选择一个
        Integer isoncyel = MapUtil.getInt(data, "isoncyel");
        if(isoncyel!=null&&isoncyel==1&&"gq_pmlist".equals(objName)){

            Integer z1 = MapUtils.getInteger(data, "Z1");
            Integer z2 = MapUtils.getInteger(data, "Z2");
            Integer z3 = MapUtils.getInteger(data, "Z3");
            Integer z4 = MapUtils.getInteger(data, "Z4");
            Integer z5 = MapUtils.getInteger(data, "Z5");
            Integer z6 = MapUtils.getInteger(data, "Z6");
            Integer z7 = MapUtils.getInteger(data, "Z7");

            if(z1==0&&z2==0&&z3==0&&z4==0&&z5==0&&z6==0&&z7==0){
                String result = "周期性开关开启后，周一到周日必须选择一个";
                return result;
            }

        }
        return super.validateSave(app,objName,data);
    }

    //计算时间
    public Date getNextDate( Date firstdate,Integer rate, String unit) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstdate);
        if (unit.equalsIgnoreCase("D")) {
            calendar.add(Calendar.DATE, rate);
        } else if (unit.equalsIgnoreCase("W")) {
            calendar.add(Calendar.WEEK_OF_MONTH, rate);
        } else if (unit.equalsIgnoreCase("M")) {
            calendar.add(Calendar.MONTH, rate);
        } else {
            calendar.add(Calendar.YEAR, rate);
        }
        return calendar.getTime();
     }


}
